/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.Util;
import net.worlds.object.GameObject;
import net.worlds.object.Player;
import net.worlds.object.PlayerStats;
import net.worlds.object.Team;

/**
 *
 * @author Jacob
 */
public class ELeaderboard extends EStickyFrame{
    private BufferedImage leaderboard;
    private Font teamFont = new Font("Arial", Font.BOLD, 16);
    private Font playerFont = new Font("Arial", 0, 15);
    private FontMetrics fm;
    private int playerHeight;
    private int teamHeight;
    private long lastUpdateTime = 0;
    public ELeaderboard(EComponent parent, int right, int top)
    {
        super(parent, 200, 200, 0, right, top, 0);
        leaderboard = Util.getGraphicsConfiguration().createCompatibleImage(100, 200, Transparency.TRANSLUCENT);
        playerHeight = (int) playerFont.getStringBounds("X", Util.fontRendererContext).getHeight();
        teamHeight = (int) teamFont.getStringBounds("X", Util.fontRendererContext).getHeight() + 2;
    }
    private void setSize(int x, int y)
    {
        setWidth(x);
        setHeight(y);
        if(x > 0 && y > 0)
            leaderboard = Util.getGraphicsConfiguration().createCompatibleImage(x, y, Transparency.TRANSLUCENT);
        else
            leaderboard = null;
    }
    public void update()
    {
        if(Main.getGame() == null || Main.getGame().getCurrentWorld() == null)
            return;
        lastUpdateTime = System.currentTimeMillis();
        try {
            HashMap<String, Team> teams = Main.getGame().getTeams();
            LinkedHashMap<Team, ArrayList<Player>> playerTeams = new LinkedHashMap<Team, ArrayList<Player>>();
            List<GameObject> players = Main.getGame().getCurrentWorld().getService("Players").getChildren();
            HashMap<String, ValueRow> values = new HashMap<String, ValueRow>();
            if(players.isEmpty())
            {
                setSize(0, 0);
                return;
            }
            int maxNameWidth = 0;
            for(GameObject o : players)
            {
                if(!(o instanceof Player))
                    continue;
                int nameWidth = (int) playerFont.getStringBounds(o.toString(), Util.fontRendererContext).getWidth();
                if(nameWidth > maxNameWidth)
                    maxNameWidth = nameWidth;
                Player p = (Player) o;
                if(p.getStats() == null)
                    continue;
                for(GameObject v : p.getStats().getValues())
                {
                    if(v.toString().equals("Team"))
                        continue;
                    int textWidth = (int) playerFont.getStringBounds(v.getProp("Value").toString(), Util.fontRendererContext).getWidth();
                    if(values.containsKey(v.toString()))
                    {
                        values.get(v.toString()).values.put(p, v.getProp("Value"));
                        if(textWidth > values.get(v.toString()).maxWidth)
                            values.get(v.toString()).maxWidth = textWidth;
                    }
                    else
                    {
                        ValueRow r = new ValueRow();
                        r.values.put(p, v.getProp("Value"));
                        int keyWidth = (int) playerFont.getStringBounds(v.toString(), Util.fontRendererContext).getWidth();
                        if(keyWidth > textWidth)
                            r.maxWidth = keyWidth;
                        else
                            r.maxWidth = textWidth;
                        values.put(v.toString(), r);
                    }
                }
            }
            Team nullTeam = new Team();
            playerTeams.put(nullTeam, new ArrayList<Player>());
            for(Team t : teams.values())
            {
                int nameWidth = (int) teamFont.getStringBounds(t.toString(), Util.fontRendererContext).getWidth();
                if(nameWidth > maxNameWidth)
                    maxNameWidth = nameWidth;
                playerTeams.put(t, new ArrayList<Player>());
            }
            int width = maxNameWidth + 20;
            for(ValueRow r : values.values())
                width += r.maxWidth + 20;
            int height = 5 + teams.size()*teamHeight + players.size() * playerHeight;
            for(GameObject o : players)
            {
                if(!(o instanceof Player))
                    continue;
                Player p = (Player) o;
                if(p.getStats() == null)
                {
                    playerTeams.get(nullTeam).add(p);
                    continue;
                }
                PlayerStats s = p.getStats();
                Object t = s.get("Team");
                if(t != null && teams.containsKey(t.toString()))
                {
                    playerTeams.get(teams.get(t.toString())).add(p);
                }
                else
                    playerTeams.get(nullTeam).add(p);
                    
            }
            if(values.size() > 0 && playerTeams.get(nullTeam).size() > 0)
                height += teamHeight;
            setSize(width, height);
            Graphics2D g = leaderboard.createGraphics();
            g.setColor(shadowColor);
            g.fillRoundRect(0, 0, getWidth(), getHeight(), 8, 8);
            int y = teamHeight - 3;
            for(Team t : playerTeams.keySet())
            {
                if(t == nullTeam && playerTeams.get(nullTeam).isEmpty())
                    continue;
                g.setFont(teamFont);
                if(t != nullTeam || values.size() > 0)
                {
                    if(t != nullTeam)
                    {
                        g.setColor(t.getColorProp("Color"));
                        g.drawString(t.toString(), 5, y);
                    }
                    else
                        g.setColor(Color.white);
                    g.drawLine(5, y+1, width-5, y+1);
                    int x = maxNameWidth + 10;
                    g.setFont(playerFont);
                    for(String value : values.keySet())
                    {
                        if(values.toString().equals("Team"))
                            continue;
                        g.drawString(value, x, y);
                        x += values.get(value).maxWidth + 20;
                    }
                    y += teamHeight;
                }
                g.setColor(Color.white);
                g.setFont(playerFont);
                for(Player p : playerTeams.get(t))
                {
                    g.drawString(p.toString(), 5, y);
                    int x = maxNameWidth + 10;
                    for(String value : values.keySet())
                    {
                        if(p.getStats() != null && p.getStats().get(value) != null)
                            g.drawString(p.getStats().get(value).toString(), x, y);
                        x += values.get(value).maxWidth + 20;
                    }
                    y += playerHeight;
                }
            }
        } catch (WorldsException ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public void render(Graphics2D g)
    {
        if(System.currentTimeMillis() - lastUpdateTime > 1000)
            update();
        g.drawImage(leaderboard, getX(), getY(), null);
    }
    class ValueRow {
        public int maxWidth;
        public HashMap<Player, Object> values = new HashMap<Player, Object>();
    }
}
